# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=tk
pkgver=8.6.15
pkgrel=0
_major=${pkgver%.*}
pkgdesc="GUI toolkit for the Tcl scripting language"
url="https://www.tcl-lang.org/"
arch="all"
options="!check"  # Requires a running X11 server.
license="TCL"
depends_dev="
	fontconfig-dev
	libx11-dev
	libxft-dev
	tcl-dev~$pkgver
	"
makedepends="$depends_dev libpng-dev"
subpackages="$pkgname-doc $pkgname-dev"
source="https://downloads.sourceforge.net/sourceforge/tcl/tk$pkgver-src.tar.gz"
builddir="$srcdir/tk$pkgver/unix"

build() {
	local _64bit="--disable-64bit"
	case "$CARCH" in
		x86_64) _64bit="--enable-64bit";;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		$_64bit
	make
}

package() {
	make INSTALL_ROOT="$pkgdir" install install-private-headers

	ln -sf wish$_major "$pkgdir"/usr/bin/wish
	install -Dm644 ../license.terms \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# remove buildroot traces
	find "$pkgdir" -name '*Config.sh' -print0 |
		xargs -0 sed -i -e "s#${srcdir}#/usr/src#"

	# move demos to -doc directory
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/examples
	mv -v "$pkgdir"/usr/lib/tk$_major/demos \
		"$pkgdir"/usr/share/doc/$pkgname/examples/
}

dev() {
	# libtk8.6.so is the only library made and it remains in the mainpackage
	# so depending on the main package automatically is never done
	# https://gitlab.alpinelinux.org/alpine/aports/issues/11232#note_68789
	depends_dev="$depends_dev tk=$pkgver-r$pkgrel"
	default_dev

	local file; find "$pkgdir" -name '*Config.sh' | while read -r file; do
		amove "${file#"$pkgdir"}"
	done
}

sha512sums="
1b4165748eb62b1ac875171036bec8c4d81ed903681806de78149e15b311c7258d9daf2bbc2a629c973a02362ad5df58157d0b90e2e54582590f62fbfc4b7d62  tk8.6.15-src.tar.gz
"
